import json
import requests
from bs4 import BeautifulSoup
import itertools
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from pylab import mpl

# 设置显示中文字体
mpl.rcParams["font.sans-serif"] = ["SimHei"]
user_agent = (
    "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/102.0.5005.63 Safari/537.36 Edg/102.0.1245.33")
url = "https://search.jd.com/Search?keyword=%E6%89%8B%E6%9C%BA&wq=%E6%89%8B%E6%9C%BA&pvid=8858151673f941e9b1a4d2c7214b2b52&page=3&s=56&click=0"

headers = {
    "user-agent": user_agent
}
resp = requests.get(url, headers=headers)
# print(resp.text)
# with open("jd.html", 'wb') as file:
#     file.write(resp.content)
#     file.close()
soup = BeautifulSoup(resp.text, "html.parser")
comments: list[str] = []
shop_name: list[str] = []
smartPhone = []
phones = []
comments_url_prefix = 'https://club.jd.com/comment/productCommentSummaries.action?referenceIds='
for tag in soup.find_all('div', class_='gl-i-wrap'):
    shop = tag.find('div', class_='p-shop').a.string if tag.find('div', class_='p-shop').find('a') else '' # 商店名
    price = tag.find('div', class_='p-price').i.string  # 价格
    prod_id = tag.findParent('li').get('data-sku')  # 商品id
    # print(prod_id)
    phone_url = 'https://item.jd.com/' + prod_id + '.html'
    # print(phone_url)
    phone_resp = requests.get(phone_url, headers=headers)
    parser = BeautifulSoup(phone_resp.text, "html.parser")
    # print(phone_resp.text)
    brand = parser.find('div', class_='item ellipsis').string
    comment_resp = requests.get(comments_url_prefix + prod_id, headers=headers)
    ct = comment_resp.json()['CommentsCount'][0]['CommentCountStr']  # 评论
    parser.find('div', class_='item ellipsis')
    phones += [[brand, shop, price, ct]]
    smartPhone += [{'品牌': brand, '店铺名称': shop, '价格': price, '评论数': ct}]
    # comments.append(str(cts))
print(smartPhone)
csv_header = ['品牌', '店铺名称', '价格', '评论数']
phone_csv = pd.DataFrame(columns=csv_header, data=phones)
phone_csv.to_csv('bafei.csv', index='false')
# for tag in soup.find_all('div', class_='p-shop'):
#     bf = tag.a.string
#     shop_name.append(str(bf))
# print(shop_name)

# print(resp.json()['data'])
# print(resp.text)

# with open("jd.json", 'w', encoding='utf-8') as file:
#     file.write(json.dumps(smartPhone, ensure_ascii=False))
#     file.close()

# 数据可视化
csv_header = ['品牌', '店铺名称', '价格', '评论数']
prod_info = pd.read_csv('bafei.csv', header=None, sep=',', encoding='utf-8')
prod_list = prod_info.values.tolist()
prod_list.pop(0)

# 计算各个店铺销售均值
sss = [
    (key, round(np.mean(list(map(lambda i: float(i[-2]), group)))))
    for key, group in itertools.groupby(sorted(prod_list, key=lambda i: i[2]), lambda i: i[2])
]


def comments_details(cts):
    if cts.find('万') != -1:
        num = cts.split('万')
        real_cts = int(num[0]) * 10000
        return real_cts
    else:
        num = cts.split('+')
        real_cts = num[0]
        return int(real_cts)


comments_top_10 = sorted(prod_list, key=lambda i: comments_details(i[-1]), reverse=1)[:10]
print(comments_top_10)
comments_num = [i[-1] for i in comments_top_10]
brands = [i[1] for i in comments_top_10]
plt.figure(figsize=(9, 7))
plt.bar(brands, [comments_details(i) for i in comments_num], label="手机品牌",
        color='lightpink')  # color也可是16进制，如上显示的  #202204
plt.legend()  # 运行结果里图例名称显示出来
plt.xticks(brands, brands, rotation=20)
print(comments_num)
for i, b in zip(comments_num, brands):
    plt.text(b, comments_details(i), i)
plt.subplots_adjust(bottom=0.15)
plt.xlabel('品牌')
plt.ylabel('评论数')
plt.title('产品评论分析')

# plt.savefig(r'E:1.png', dpi=1000, bbox_inches='tight')  # 保存至本地
plt.show()

plt.pie(list(map(lambda i: i[1], sss)), labels=list(map(lambda i: i[0], sss)), autopct='%0.2f%%')
plt.show()
